package com.sgb.kjwl.view.ui.activity.material.hall;

import com.sgb.kjwl.api.APIManager;
import com.sgb.jicai.entity.CentralizedMiningEntity;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.view.entity.MenuEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.swgk.core.util.PickSelectEntity;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class XufangFaQiModle extends BaseViewModel {

    private XufangFaQiFragment fragment;
    private int page = 1;
    private Map map;

    public XufangFaQiModle(XufangFaQiFragment fragment) {
        this.fragment = fragment;
    }


    /**
     * @param provinceNo   省
     * @param cityNo       市
     * @param jicaiMethod  集采方式（一级tab：0全部，1即报即采，2汇集采购,3企业专项,4集采锁价）
     * @param jicaiType    集采类型（二级tab：0全部，1商品集采 2 机械集采 3 设备集采 4 劳务集采 5房产集采 6租赁集采）
     * @param statusNo     集采状态
     * @param allSearchKey 搜索词
     * @param isLoadMore   是否加载更多
     */
    public void getJiCaiList(String provinceNo, String cityNo, String jicaiMethod, int jicaiType, String statusNo, String firstTypeNo, String secondTypeNo, String allSearchKey, String endDayType, boolean isLoadMore) {
        map = new HashMap();
        page = isLoadMore ? page + 1 : 1;
        map.put("limit", 10);
        map.put("current", page);
        map.put("provinceNo", provinceNo);//（省）
        map.put("cityNo", cityNo);//市级编号
        map.put("groupBuyType", jicaiMethod.equals("0") ? "" : jicaiMethod);//集采方式  0  全部   1.即报即采  2.汇集采购  3.企业专项
        map.put("groupBuyMode", "1");//集采类型：1,需方集采，2企业专项
        map.put("groupbuyClass", jicaiType == 0 ? "" : jicaiType);//集采类型  商品、机械、设备
        map.put("status", statusNo);
        map.put("typeNo", firstTypeNo);//集采类别
        map.put("endDayType", endDayType);//集采类别
        if (null != secondTypeNo && !"".equals(secondTypeNo)) {
            map.put("typeNo", secondTypeNo);//最后一级类目
        }
        map.put("searchKey", allSearchKey);//搜索内容
        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getCentralizedMiningAPI().getJiCaiList(map).enqueue(new Callback<BaseEntity<ListResponse<List<CentralizedMiningEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<CentralizedMiningEntity>>>> call, Response<BaseEntity<ListResponse<List<CentralizedMiningEntity>>>> response) {
                if (response.body() == null || response.body().getState() == null) {
                    DialogHelper.dismissProgressDialog();
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (!isLoadMore) {
                        fragment.showList(response.body().getData().getList(), response.body().getData().isLastPage());
                    } else {
                        fragment.loadMore(response.body().getData().getList(), response.body().getData().isLastPage());
                    }
                } else {
                    MToast.showToast(fragment.getContext(), response.body().getMsg());
                }
                DialogHelper.dismissProgressDialog();
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<CentralizedMiningEntity>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void getH5Url(String id) {
        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPickAPI().getUrlSetting("collection").enqueue(new Callback<BaseEntity<H5ReEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<H5ReEntity>> call, Response<BaseEntity<H5ReEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {//type 0 首页 1 我的集采 2 我的报价
                    SharedPreferenceUtil.init(fragment.getActivity());
                    String token = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.resourceAPPToken, "");
//                    String url = response.body().getData().getUrl() + "?id=" + id + "&type=0" + "&token=" + repertory.getToken();
                    String url = response.body().getData().getUrl() + "?id=" + id + "&type=0" + "&token=" + token;
                    fragment.showUrl(url);
                } else {
                    MToast.showToast(fragment.getContext(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<H5ReEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }


    //获取省份数据
    public void getProvinceData() {
        Map map = new HashMap();
        SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.LONGITUDE_ID, "");
        map.put("areaCode", "");
        APIManager.getInstance().getPickAPI().getLocationNew(map).enqueue(new Callback<BaseEntity<List<PickSelectEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<PickSelectEntity>>> call, Response<BaseEntity<List<PickSelectEntity>>> response) {
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    fragment.setProvinceDatas(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<PickSelectEntity>>> call, Throwable t) {
            }
        });
    }

    public void getCityData(String province) {
        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPickAPI().getAreaSecond(province).enqueue(new Callback<BaseEntity<List<PickSelectEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<PickSelectEntity>>> call, Response<BaseEntity<List<PickSelectEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    fragment.setCityDatas(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<PickSelectEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    //获取集采状态（根据集采方式）
    public void getStatusListByType(String groupType) {
        String type = groupType.equals("0") ? "" : groupType;
        APIManager.getInstance().getCentralizedMiningAPI().getStatusList(type).enqueue(new Callback<BaseEntity<List<PickSelectEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<PickSelectEntity>>> call, Response<BaseEntity<List<PickSelectEntity>>> response) {
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    fragment.setStatusDatas(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<PickSelectEntity>>> call, Throwable t) {

            }
        });
    }

    //根据一级分类获取二级分类 type为一级类型(接口返回为三级，目前只用到第二级)
    public void getTypeDatesList(String parentNo, String groupBuyClass) {
        if ("0".equals(parentNo) && "".equals(groupBuyClass)) {
            return;
        }
        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getCentralizedMiningAPI().getSecondCategorys(parentNo, groupBuyClass).enqueue(new Callback<BaseEntity<List<MenuEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<MenuEntity>>> call, Response<BaseEntity<List<MenuEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (parentNo.equals("0")) {
                        fragment.setTypesFirstDatas(response.body().getData());
                    } else {
                        fragment.setTypesSecondDatas(response.body().getData());
                    }

                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<MenuEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    //截止时间
    public void getDateTimeList() {
        APIManager.getInstance().getCentralizedMiningAPI().getFactoryDateTimeList().enqueue(new Callback<BaseEntity<List<PickSelectEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<PickSelectEntity>>> call, Response<BaseEntity<List<PickSelectEntity>>> response) {
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    fragment.setTimeDatas(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<PickSelectEntity>>> call, Throwable t) {
            }
        });
    }
}
